<?php
/* $Id: search_results.issues.php,v 1.6 2003/10/09 16:46:22 tuxmonkey Exp $ */
/**
 * @package Issue-Tracker
 * @subpackage Issues
 */

if (strpos($_SERVER['PHP_SELF'],basename(__FILE__)) !== false) {
  print "Direct module access forbidden.";exit;}
  

if(empty($_POST['criteria'])){
  $_POST['criteria'] = "%";
}

if(empty($_POST['sort'])){
  $_POST['sort'] = "issueid";
}

if (@count($_POST['groups']) < 1
or empty($_POST['groups'])) {
  $_POST['groups'] = $_SESSION['groups'];
}

$issues = array();

$links[] = array(
  "img" => $_ENV['imgs']['search'],
  "txt" => $lg->mm_new_search,
  "url" => "?module=issues&action=search"
);


$sql  = "SELECT i.issueid ";
$sql .= "FROM issues i ";
$sql .= "LEFT JOIN issue_groups g ON i.issueid = g.issueid ";
$sql .= "LEFT JOIN events e ON i.issueid = e.issueid ";
$sql .= "WHERE (i.descr LIKE LOWER('%".$_POST['criteria']."%') ";
$sql .= "OR i.objet LIKE LOWER('%".$_POST['criteria']."%') ";
$sql .= "OR e.action LIKE LOWER('%".$_POST['criteria']."%')) ";
if( !empty($_POST['issueid'])) $sql .= "AND i.issueid = '".$_POST['issueid']."' ";

$sql .= is_array($_POST['groups']) ? "AND g.gid IN (".implode(",",$_POST['groups']).") " : "";
$sql .= is_array($_POST['opened']) ? "AND i.opened_by IN (".implode(",",$_POST['opened']).") " : "";
$sql .= is_array($_POST['assigned']) ? "AND g.assigned_to IN (".implode(",",$_POST['assigned']).") " : "";
$sql .= is_array($_POST['status']) ? "AND i.status IN (".implode(",",$_POST['status']).") " : "";
$sql .= is_array($_POST['category']) ? "AND i.category IN (".implode(",",$_POST['category']).") " : "";
$sql .= is_array($_POST['product']) ? "AND i.product IN (".implode(",",$_POST['product']).") " : "";
// add JOP
$sql .= is_array($_POST['severity']) ? "AND i.severity IN (".implode(",",$_POST['severity']).") " : "";
// end add
$sql .= "GROUP BY i.issueid " ;
$sql .= "ORDER BY i.".$_POST['sort']." ASC";

$uissues = $dbi->fetch_all($sql,'uni');

if($uissues!= '')
	$issues = $dbi->fetch_all("SELECT * FROM issues WHERE issueid IN ($uissues) ","array");

// add JOP
  $colspan = count($_SESSION['prefs']['show_fields']) + 3;
  $title = $lg->search_results;
  $smarty->assign('colspan',$colspan);
  $smarty->assign('title',$title);
// end add

$smarty->assign('url',"?module=issues&action=search_results");
$smarty->assign('issues',$issues);
$smarty->display("issues/search_results.tpl");
?>